﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for CItem
/// </summary>
public class CItem
{
	public CItem()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public bool Add(string title,int parent,string content)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object,object> pars=new Dictionary<object,object>();
        pars.Add("Parent",parent);
        object obj=d.ExeScalar("select count(*) from item where  parent=@Parent",pars);
        pars.Add("SortCode", (Convert.ToInt32(obj) + 1));
        pars.Add("Title", title);
        pars.Add("Content", content);
        int c = d.ExeNonQuery("insert into item(parent,sortcode,title,content) values(@Parent,@SortCode,@Title,@Content)", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool Update(int id,string title,string content)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Title", title);
        pars.Add("Content", content);
        pars.Add("ID", id);
        int c = d.ExeNonQuery("update item set title=@Title,content=@Content where id=@ID", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool Delete(int id)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from item where id=@ID", pars);
        int c=d.ExeNonQuery("delete from item where id=@ID", pars);
        pars.Clear();
        pars.Add("Parent",Convert.ToInt32( dt.Rows[0]["Parent"].ToString()));
        pars.Add("SortCode",Convert.ToInt32(dt.Rows[0]["SortCode"].ToString()));
        c+=d.ExeNonQuery("update item set sortcode=sortcode-1 where  parent=@Parent and sortcode>@SortCode", pars);
        d.Close();
        if (c> 0)
        {
            return true;
        }
        return false;
    }

    public bool Sort(int id,bool isUp)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from item where id=@ID", pars);
        pars.Clear();
        pars.Add("Parent", dt.Rows[0]["Parent"].ToString());
        pars.Add("SortCode", Convert.ToInt32(dt.Rows[0]["SortCode"].ToString()));
        if (isUp)
        {
            dt = d.ExeDataTable("select top 1 * from item where  parent=@Parent and sortcode<@SortCode order by sortcode desc", pars);
        }
        else
        {
            dt = d.ExeDataTable("select top 1 * from item where  parent=@Parent and sortcode >@SortCode order by sortcode asc", pars);
        }
        pars.Clear();
        pars.Add("ID", id);
        int c=d.ExeNonQuery("update item set sortcode="+(isUp ? "sortcode-1":"sortcode+1")+" where id=@ID",pars);
        pars.Clear();
        if (null != dt)
        {
            pars.Add("ID", Convert.ToInt32(dt.Rows[0]["id"].ToString()));
            c+=d.ExeNonQuery("update item set sortcode="+(isUp ? "sortcode+1":"sortcode-1")+" where id=@ID",pars);
        }
        d.Close();
        if (c> 0)
        {
            return true;
        }
        return false;
    }

    public DataTable ReadItems(int parent)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Parent", parent);
        DataTable dt = d.ExeDataTable("select * from item where parent=@Parent order by sortcode asc", pars);
        d.Close();
        return dt;
    }

    public DataRow ReadItem(int id)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from item where id=@ID", pars);
        d.Close();
        if (null != dt)
        {
            return dt.Rows[0];
        }
        return null;
    }
}